<script setup>
import {onMounted, ref} from 'vue'
import axios from "axios";
import {ElMessage} from "element-plus";

// 默认激活的标签页

const Units = ref([])

const pageSize = ref(10)
const currentPage = ref(1)
const total = ref(1)
const handleSizeChange=(newSize)=>{
  pageSize.value = newSize
  list();

};
const handleCurrentChange=(newPage)=> {
  list();

};

// 删除方法
const handleDelete = (row) => {
  axios.delete(`/carcenter-service-order/units/delete/${row.id}`).then(res => {
    // 提交表单逻辑
    ElMessage.success('删除成功');
    list();
  })

}
//获取分页信息
const list = () => {
   axios.get(`/carcenter-service-order/units/getDetail/${currentPage.value}/${pageSize.value}`).then((res) => {
    Units.value = res.data.data.data;
    total.value = res.data.data.total;});
}
//新增
const dialogVisible = ref(false);
const unitFormRef = ref(null);

const unitForm = ref({});

const rules = {
  unitName: [
    { required: true, message: '请输入单位名称', trigger: 'blur' }
  ]
};

const openDialog = () => {
  dialogVisible.value = true;
};

const closeDialog = () => {
  dialogVisible.value = false;
  unitFormRef.value.resetFields();
};
//新增
const submitForm = (type) => {
  unitFormRef.value.validate((valid) => {
    if (valid) {
        axios.post('/carcenter-service-order/units/addOrEdit', unitForm.value).then(res => {
          // 提交表单逻辑
          ElMessage.success('汽车品牌添加成功');
          closeDialog();
          list();
        })
    } else {
      ElMessage.error('请检查输入信息');
      return false;
    }
  });
};
onMounted(()=>{
  list();
})

</script>

<template>
  <el-button @click="openDialog" type="primary"><el-icon><Plus /></el-icon>新增</el-button>
      <el-table :data="Units" style="width: 100%">
        <el-table-column prop="unitName" label="单位名称" width="180"></el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button size="mini" type="danger" @click="handleDelete(scope.row,)"><el-icon><Delete /></el-icon>删除</el-button>
          </template>
        </el-table-column>
      </el-table>
  <!-- 新增品牌弹窗 -->
  <el-dialog v-model="dialogVisible" title="新增单位" width="30%">
    <el-form :model="unitForm" :rules="rules" ref="unitFormRef">
      <el-form-item label="" prop="unitName">
        <el-input v-model="unitForm.unitName" placeholder="请输入单位名称"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取消</el-button>
          <el-button type="primary" @click="submitForm">确认</el-button>
        </span>
    </template>
  </el-dialog>

    <!-- 分页 -->
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-size-options="[10, 15, 20]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
    />

</template>

